﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Bovril.PlayingCards.French;

namespace Bovril.Poker
{
    public static class HandFactory
    {
        public static Hand CreateHand(Card card1, Card card2, Card card3, Card card4, Card card5)
        {
            int hash = GetHandHash(card1, card2, card3, card4, card5);

            if (cachedHands_.ContainsKey(hash))
            {
                return cachedHands_[hash];
            }

            Hand newHand = new Hand(card1, card2, card3, card4, card5);
            cachedHands_.Add(hash, newHand);

            return newHand;
        }

        private static int GetHandHash(Card card1, Card card2, Card card3, Card card4, Card card5)
        {
            int hash = 0;
            hash += (int)card1.Suit << 0;
            hash += (int)card1.Rank << 2;
            hash += (int)card2.Suit << 6;
            hash += (int)card2.Rank << 8;
            hash += (int)card3.Suit << 12;
            hash += (int)card3.Rank << 14;
            hash += (int)card4.Suit << 18;
            hash += (int)card4.Rank << 20;
            hash += (int)card5.Suit << 24;
            hash += (int)card5.Rank << 26;

            return hash;
        }

        private static Dictionary<int, Hand> cachedHands_ = new Dictionary<int, Hand>();
    }
}
